package org.jeecg.modules.xxgx.fjys.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.xxgx.fjys.entity.CsXxFjys;
import org.jeecg.modules.xxgx.fjys.service.ICsXxFjysService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

 /**
 * @Description: 附加要素
 * @Author: jeecg-boot
 * @Date:   2023-10-09
 * @Version: V1.0
 */
@Api(tags="附加要素")
@RestController
@RequestMapping("/xxgx/fjys/csXxFjys")
@Slf4j
public class CsXxFjysController extends JeecgController<CsXxFjys, ICsXxFjysService> {
	@Autowired
	private ICsXxFjysService csXxFjysService;

	/**
	 * 分页列表查询
	 *
	 * @param csXxFjys
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "附加要素-分页列表查询")
	@ApiOperation(value="附加要素-分页列表查询", notes="附加要素-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<CsXxFjys>> queryPageList(CsXxFjys csXxFjys,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<CsXxFjys> queryWrapper = QueryGenerator.initQueryWrapper(csXxFjys, req.getParameterMap());
		LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		Page<CsXxFjys> page = new Page<CsXxFjys>(pageNo, pageSize);
		queryWrapper.eq("company_id", user.getCompanyId());
		queryWrapper.orderByDesc("create_time");
		IPage<CsXxFjys> pageList = csXxFjysService.page(page, queryWrapper);
		csXxFjysService.handleYyzt(pageList.getRecords());
		return Result.OK(pageList);
	}

	/**
	 *   添加
	 *
	 * @param csXxFjys
	 * @return
	 */
	@AutoLog(value = "附加要素-添加")
	@ApiOperation(value="附加要素-添加", notes="附加要素-添加")
	@RequiresPermissions("xxgx.fjys:cs_xx_fjys:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody CsXxFjys csXxFjys) {
		LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		csXxFjys.setCompanyId(user.getCompanyId());

		QueryWrapper<CsXxFjys> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("company_id",user.getCompanyId());
		queryWrapper.eq("xmmc",csXxFjys.getXmmc());
		int count = (int)csXxFjysService.count(queryWrapper);
		if(count > 0){
			throw new JeecgBootException(csXxFjys.getXmmc() + " 已存在！");
		}
		csXxFjysService.FjysSave(csXxFjys);
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param csXxFjys
	 * @return
	 */
	@AutoLog(value = "附加要素-编辑")
	@ApiOperation(value="附加要素-编辑", notes="附加要素-编辑")
	@RequiresPermissions("xxgx.fjys:cs_xx_fjys:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody CsXxFjys csXxFjys) {
		LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		csXxFjys.setCompanyId(user.getCompanyId());
		csXxFjysService.FjysEdit(csXxFjys);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "附加要素-通过id删除")
	@ApiOperation(value="附加要素-通过id删除", notes="附加要素-通过id删除")
	@RequiresPermissions("xxgx.fjys:cs_xx_fjys:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		csXxFjysService.FjysDelete(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "附加要素-批量删除")
	@ApiOperation(value="附加要素-批量删除", notes="附加要素-批量删除")
	@RequiresPermissions("xxgx.fjys:cs_xx_fjys:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		csXxFjysService.FjysBathDelete(ids);
		//this.csXxFjysService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	@AutoLog(value = "附加要素-同步局端数据")
	@ApiOperation(value="附加要素-同步局端数据", notes="附加要素-同步局端数据")
	@PostMapping(value = "/syncElecTax")
	public Result<String> syncElecTax() {
		//csXxFjysService.FjysBathDelete(ids);
		csXxFjysService.syncElecTax();
		return Result.OK("同步成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "附加要素-通过id查询")
	@ApiOperation(value="附加要素-通过id查询", notes="附加要素-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<CsXxFjys> queryById(@RequestParam(name="id",required=true) String id) {
		CsXxFjys csXxFjys = csXxFjysService.getById(id);
		if(csXxFjys==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(csXxFjys);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param csXxFjys
    */
    @RequiresPermissions("xxgx.fjys:cs_xx_fjys:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, CsXxFjys csXxFjys) {
        return super.exportXls(request, csXxFjys, CsXxFjys.class, "附加要素");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequiresPermissions("xxgx.fjys:cs_xx_fjys:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, CsXxFjys.class);
    }

}
